﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.LibarySys
{
    public partial class ReturnScrap : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Request["libraryOutMaterial_id"] != null)
                {
                    int libraryOutMaterial_id = int.Parse(Request["libraryOutMaterial_id"]);
                    BindDate(libraryOutMaterial_id);
                }

                //if (Request["Return_num"] != null)
                //{
                //    MaterialNum.Text = Request["Return_num"].ToString();
                //}
            }
        }

        protected void BindDate(int libraryOutMaterial_id)
        {
            BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL lbll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL lommodel = new Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            lommodel = lbll.GetModel(libraryOutMaterial_id);
            hidMaterialID.Value = lommodel.MATERIAL_ID.ToString();
            MaterialName.Text = lommodel.MATERIAL_NAME;
            hidMaterialName.Value = lommodel.MATERIAL_NAME;
            MaterialNum.Text = Request["Return_num"].ToString();
            hidNum.Value = Request["Return_num"].ToString();
            string libraryOut_id = lommodel.LIBRARYOUTID;
            Model.LibrarySys.T_PUB_LIBRARYOUT libraryout = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            libraryout = bll.GetModel(libraryOut_id);
            Applicants.Text = libraryout.APPLICANT_NAME;
            hidApplicantName.Value = libraryout.APPLICANT_NAME;
            hidApplicant.Value = libraryout.APPLICANT.ToString();
            Approver.Text = Session["USER_NAME"].ToString();
            int branch_id = int.Parse(libraryout.BRANCH_ID.ToString());
            hidBranchID.Value = libraryout.BRANCH_ID.ToString();
            BindBranchName(branch_id);
            int library_id = int.Parse(libraryout.LIBRARY_ID.ToString());
            hidLibraryID.Value = libraryout.LIBRARY_ID.ToString();
            //hidLibraryID.Value = library_id.ToString();
            BindLibraryName(library_id);
        }

        #region 绑定仓库
        private void BindLibraryName(int library_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            Model.SysManage.T_PUB_LIBRARY library = bll.GetModel(library_id);
            if (library != null)
            {
                this.LibrayName.Text = library.LIBRARY_NAME;
                //this.LibraryID.Value = library.LIBRARY_ID.ToString();
            }
        }
        #endregion

        #region 绑定分店列表
        private void BindBranchName(int branch_id)
        {
            Model.SysManage.T_PUB_BRANCH branch = new Model.SysManage.T_PUB_BRANCH();
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            branch = LibraryBll.GetModel(branch_id);
            BranchName.Text = branch.BRANCH_NAME;
        }
        #endregion

        protected void btnPass_Click(object sender, EventArgs e)
        {
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            Model.LibrarySys.T_PUB_SCRAP model = new Model.LibrarySys.T_PUB_SCRAP();
            model.APPLICANT = int.Parse(hidApplicant.Value);
            model.BRANCH_ID = int.Parse(hidBranchID.Value);
            model.LIBRARY_ID = int.Parse(hidLibraryID.Value);
            model.MATERIAL_ID = int.Parse(hidMaterialID.Value);
            model.TITEL = applytitel.Value;
            model.REMARK = Remark.Text;
            model.NUM = int.Parse(MaterialNum.Text);
            model.MATERIAL_NAME = MaterialName.Text;
            model.STATE = "3";
            model.APPROVER = int.Parse(Session["USER_ID"].ToString());
            model.APPROVER_NAME = Session["USER_NAME"].ToString();
            model.APPLY_TIME = DateTime.Now;
            model.APPROVE_TIME = model.APPLY_TIME;
            model.FLAG = "1";
            int scrap_id = bll.Add(model);
            if (scrap_id != 0)
            {
                string strJS = "SelectClick('" + scrap_id + "')";
                Universal.JS.RunJS(strJS);
            }
            else
            {
                Universal.JS.Alert("申请不成功！请您重新申请！");
            }
        }
    }
}